import csv
from peewee import *

db = MySQLDatabase("fb_data", host="127.0.0.1", port=3306, user="root", password="123456")


class BaseModel(Model):
    class Meta:
        database = db


class Interview(BaseModel):
    id = IntegerField()
    category_id = IntegerField()
    category_name = CharField()
    question = TextField(null=False)
    difficulty = IntegerField(null=True)
    analysis = TextField(null=True)
    answer = TextField(null=True)
    source = TextField(null=True)


if __name__ == "__main__":

    # 获取所有用户
    interview = Interview.select()

    # 将数据导出到CSV文件
    with open('interview.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(['题目id', '类别id', '类别名', '题目', '难度', '分析', '答案', '来源'])  # 写入表头
        for interv in interview:
            writer.writerow([interv.id,
                             interv.category_id,
                             interv.category_name,
                             interv.question,
                             interv.difficulty,
                             interv.analysis,
                             interv.answer,
                             interv.source])

